package com.example.lancooznbk.view.base;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.OvershootInterpolator;
import android.widget.TextView;

import com.example.lancooznbk.R;
import com.example.lancooznbk.presenter.presenter_interface.ILoadHomeDataPresenter;
import com.example.lancooznbk.utils.DensityUtils;
import com.example.lancooznbk.utils.SwipRefreshHelper;
import com.example.lancooznbk.utils.TextUtils;
import com.example.lancooznbk.view.widget.DecoratorXRefreshView;
import com.example.lancooznbk.view.widget.EmptyMainLayout;

/**
 * Created by Administrator on 2018\2\6 0006.
 */

public abstract class ZnbkBaseMainFragment<T extends ILoadHomeDataPresenter> extends BaseFragment {
    protected DecoratorXRefreshView mXRefreshView;
    public T mPresenter;
    public Handler  ZnbkBasehandler= new Handler();
    public Runnable ZnbkBaserunnable = new Runnable() {
        public void run() {
            Log.e("ZnbkBaserunnable","ZnbkBaserunnable+");
            mXRefreshView.stopRefresh();

        }
    };
    private int lastOffset;
    private int lastPosition;
    protected RecyclerView mRecyclerView;

    @Nullable
    protected FloatingActionButton mFAB;

    public abstract void initBaseMainViews(View view, Bundle savedInstanceState);

    @Override
    public void initViews(View view, Bundle savedInstanceState) {
        Log.e("aaaaaaaainitViews1","1111111111");
        mFAB = (FloatingActionButton) view.findViewById(R.id.fab_coping);
        if (mFAB != null) {
            mFAB.setImageBitmap(TextUtils.textAsBitmap("错题", DensityUtils.sp2px(getHolderActivity(), 18), Color.WHITE));
            mFAB.setTranslationY(2 * getResources().getDimensionPixelOffset(R.dimen.btn_fab_size));
        }
       /* mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_main);
        LinearLayoutManager linearLayoutManager = new WrapContentLinearLayoutManager(getActivity(),
                LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                int [] positionAndOffset = SwipRefreshHelper.getPositionAndOffset(recyclerView);
                lastPosition = positionAndOffset[0];
                lastOffset = positionAndOffset[1];
                getPositionAndOffset();
            }
        });*/
        Log.e("111111","2222");
        mXRefreshView = (DecoratorXRefreshView) view.findViewById(R.id.xrv);
//        mXRefreshView.dispatchTouchEvent(MotionEvent ev){
//            final int action = ev.getAction();
//            int deltaY = 0;
//            int deltaX = 0;
//            switch (action) {
//                case MotionEvent.ACTION_DOWN:
//                    break;
//                break;
//            }
//            return super.dispatchTouchEvent(ev);
//
//
//        }
        mEmptyViewLayout = new EmptyMainLayout(getActivity());
        SwipRefreshHelper.init(mXRefreshView,false,mEmptyViewLayout,new DecoratorXRefreshView.SimpleXRefreshListener(){
            @Override
            public void onRefresh(boolean isPullDown) {
                Log.e("SwipRefreshHelper.init","isPullDown+"+isPullDown);
                mPresenter.getData(!isPullDown);
                ZnbkBasehandler.postDelayed(ZnbkBaserunnable ,20000);

            }

            @Override
            public void onLoadMore(boolean isSilence) {
                super.onLoadMore(isSilence);
                mPresenter.getMoreData();
            }
            @Override
            public void onHeaderMove(double offset, int offsetY) {
                Log.e("onHeaderMove","offset+"+offset+"+offsetY+"+offsetY);
            }
        });
        Log.e("3333333333333","4444444444");
        initBaseMainViews(view,savedInstanceState);
    }


    /**
     * 记录RecyclerView当前位置
     */
    private void getPositionAndOffset() {
        Log.e("getPositionAndOffset","getPositionAndOffset");
        LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
        //获取可视的第一个view
        View topView = layoutManager.getChildAt(0);
        if(topView != null) {
            //获取与该view的顶部的偏移量
            lastOffset = topView.getTop();
            //得到该View的数组位置
            lastPosition = layoutManager.getPosition(topView);
        }
        Log.e("getPositionAndOffset1","getPositionAndOffset1");

    }


    @Override
    public void showLoading() {
        Log.e("showLoading","showLoading");
        super.showLoading();
        mXRefreshView.enableEmptyView(true);
        Log.e("showLoading1","showLoading1");
    }

    @Override
    public void showNetError() {
        Log.e("showNetError","showNetError");
        super.showNetError();
        mXRefreshView.enableEmptyView(true);
        mXRefreshView.stopRefresh();
        mXRefreshView.stopLoadMore();
        showFab();
        Log.e("showNetError1","showNetError1");
    }

    @Override
    public void hideLoading() {
        Log.e("hideLoading","hideLoading");
        super.hideLoading();
        mXRefreshView.enableEmptyView(false);
        mXRefreshView.stopRefresh(true);
        Log.e("hideLoading1","hideLoading1");
    }

    @Override
    public void showNoData(String msg) {
        Log.e("showNoData","showNoData");
        super.showNoData(msg);
        mXRefreshView.enableEmptyView(true);
        mXRefreshView.stopRefresh();
        showFab();
        Log.e("showNoData1","showNoData1");
    }


    public void loadComplete(boolean hasComplete) {
        Log.e("loadComplete","loadComplete");
        mXRefreshView.setPullLoadEnable(true);
        mXRefreshView.setLoadComplete(hasComplete);
        //mXRefreshView.stopRefresh();
        Log.e("loadComplete1","loadComplete1");
    }


    protected void initFragmentToolBar(Toolbar toolbar, boolean homeAsUpEnabled, String title){
        Log.e("initFragmentToolBar","initFragmentToolBar");
        toolbar.setTitle("");
        ((TextView)toolbar.findViewById(R.id.tv_toolbar_title)).setText(title);
        Log.e("initFragmentToolBar1","initFragmentToolBar1");
    }

    protected void initFragmentToolBar(Toolbar toolbar, boolean homeAsUpEnabled, int resTitle){
        Log.e("initFragmentToolBar","initFragmentToolBar");
        toolbar.setTitle("");
        ((TextView)toolbar.findViewById(R.id.tv_toolbar_title)).setText(getString(resTitle));
        Log.e("initFragmentToolBar1","initFragmentToolBar1");
    }

    private void showFab(){//第一次进来时
        Log.e("showFab","showFab");
        if (mFAB != null && mFAB.getTranslationY() > 0) {
            mFAB.animate()
                    .translationY(0)
                    .setInterpolator(new OvershootInterpolator(1.f))
                    .setStartDelay(300)
                    .setDuration(800)
                    .start();
        }
        Log.e("showFab1","showFab1");
    }


    //数据绑定是异步的，监听数据绑定成功的回调
    protected ViewTreeObserver.OnGlobalLayoutListener layoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Log.e("OnGlobalLayoutListener","OnGlobalLayoutListener");
            showFab();
            if(mRecyclerView.getLayoutManager() != null && lastPosition >= 0) {
                ((LinearLayoutManager) mRecyclerView.getLayoutManager()).scrollToPositionWithOffset(lastPosition, lastOffset);
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                mRecyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(layoutListener);
            }
            Log.e("OnGlobalLayoutListener","layoutListener1");
        }
    };



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.e("onActivityResult","onActivityResult");
        super.onActivityResult(requestCode, resultCode, data);
        mPresenter.getData(true);
        Log.e("onActivityResult1","onActivityResult1");
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {//页面可见时FloatingBarButton按钮显示
        super.setUserVisibleHint(isVisibleToUser);
        /*if (isVisibleToUser){
            if (mFAB != null && mFAB.getVisibility() == View.GONE){
                ViewHelper.animateIn(mFAB);
            }
        }*/
    }
}